package com.blinddate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blinddate.context.BaseContext;
import com.blinddate.mapper.InterestMapper;
import com.blinddate.mapper.RelevancyMapper;
import com.blinddate.pojo.entity.Interest;
import com.blinddate.pojo.entity.Relevancy;
import com.blinddate.pojo.entity.UserInf;
import com.blinddate.pojo.result.Result;
import com.blinddate.pojo.vo.UserInfVO;
import com.blinddate.service.RelevancyService;
import com.blinddate.service.UserInfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class RelevancyServiceImpl extends ServiceImpl<RelevancyMapper, Relevancy> implements RelevancyService {
    @Autowired
    private UserInfService userInfService;

    @Autowired
    private InterestMapper interestMapper;


    @Override
    public List<UserInfVO> getUserInfo(Integer page, Integer size) {
        Long userId = BaseContext.getCurrentId();
        IPage iPage = new Page(page,size);
        LambdaQueryWrapper<Relevancy> relevancyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        relevancyLambdaQueryWrapper.eq(Relevancy::getUserId,userId);
        this.page(iPage,relevancyLambdaQueryWrapper);
        List<Relevancy> list = iPage.getRecords();
        List<UserInfVO> userInfvoList = new ArrayList<>();
        for (Relevancy relevancy :list) {//获取解锁用户的信息
            LambdaQueryWrapper<UserInf> userInfLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userInfLambdaQueryWrapper.eq(UserInf::getUserId,relevancy.getGetId());
            UserInf userInf = userInfService.getOne(userInfLambdaQueryWrapper);
            if (userInf!=null){
                UserInfVO userInfVO = new UserInfVO();
                userInfVO.setUserInf(userInf);
                userInfService.showAllInf(userInfVO,userId,relevancy.getGetId());
                userInfvoList.add(userInfVO);
            }
        }
        return userInfvoList;
    }

    @Override
    public List<UserInfVO> getWhoUserInfo(Integer page, Integer size) {//获取关注列表
        Long userId = BaseContext.getCurrentId();
        IPage iPage = new Page(page,size);
        //被解锁的条件判断
        LambdaQueryWrapper<Relevancy> relevancyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        relevancyLambdaQueryWrapper.eq(Relevancy::getGetId,userId);
        this.page(iPage, relevancyLambdaQueryWrapper);
        //获取到被解锁用户列表
        List<Relevancy> list = iPage.getRecords();
        List<UserInfVO> userInfvoList = new ArrayList<>();
        for (Relevancy relevancy :list) {//根据被解锁用户信息列表获取用户信息并做出相关字段的加入
            //获取用户信息
            log.info("进入，参数为relevancy:{}",relevancy);
            LambdaQueryWrapper<UserInf> userInfLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userInfLambdaQueryWrapper.eq(UserInf::getUserId,relevancy.getUserId());
            UserInf userInf = userInfService.getOne(userInfLambdaQueryWrapper);
            if (userInf!=null){
                UserInfVO userInfVO = new UserInfVO();
                userInfVO.setUserInf(userInf);
                //设置相关字段
                UserInfVO userInfVO1 = userInfService.showAllInf(userInfVO, userId, relevancy.getUserId());
                userInfvoList.add(userInfVO1);
            }
        }
        return userInfvoList;
    }
}
